package org.wmq.organizationmanagement.controller;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;
import org.wmq.organizationmanagement.common.Result;
import org.wmq.organizationmanagement.model.dto.OrganizationMemberDTO;
import org.wmq.organizationmanagement.model.po.OrganizationMember;
import org.wmq.organizationmanagement.model.vo.MemberVO;
import org.wmq.organizationmanagement.service.OrganizationMemberService;

import java.util.List;

@RestController
@RequestMapping("organization-member")
@RequiredArgsConstructor
@Slf4j
public class OrganizationMemberController {
    private final OrganizationMemberService organizationMemberService;
    @GetMapping("/{organizationId}")
    public Result<List<MemberVO>> getMemberListByOrganizationId(@PathVariable Long organizationId){
        List<MemberVO> resultList = this.organizationMemberService.getListByOrganizationId(organizationId);
        if (resultList == null || resultList.isEmpty()){
            log.error("未查到任何会员");
            return Result.error("未查到任何会员");
        }
        log.info("查询到会员：{}", resultList);
        return Result.ok(resultList);
    }
    @GetMapping("/{organizationId}/{memberName}")
    public Result<List<MemberVO>> getMemberListByOrganizationIdAndMemberName(@PathVariable Long organizationId, @PathVariable String memberName){
        List<MemberVO> resultList = this.organizationMemberService.getListByOrganizationIdAndMemberName(organizationId, memberName);
        if (resultList == null || resultList.isEmpty()){
            log.error("未查到任何同名会员");
            return Result.error("未查到任何同名会员");
        }
        log.info("查询到同名会员：{}", resultList);
        return Result.ok(resultList);
    }
    @DeleteMapping("/{organizationId}/{memberId}")
    public Result<Void> deleteByOrganizationAndMemberId(@PathVariable Long organizationId, @PathVariable Long memberId){
        boolean deleteSuccess = this.organizationMemberService.deleteByOrganizationAndMemberId(organizationId, memberId);
        if (!deleteSuccess){
            log.error("删除organization-member失败");
            return Result.error("删除organization-member失败");
        }
        log.info("删除organization-member成功");
        return Result.ok();
    }
    @DeleteMapping("/{organization}")
    public Result<Void> deleteByOrganizationId(@PathVariable Long organizationId){
        boolean deleteSuccess = this.organizationMemberService.deleteByOrganizationID(organizationId);
        if (!deleteSuccess){
            log.error("删除organization-member失败");
            return Result.error("删除organization-member失败");
        }
        log.info("删除organization-member成功");
        return Result.ok();
    }
    @DeleteMapping("/deleteByMemberId/{memberId}")
    public Result<Void> deleteByMemberId(@PathVariable Long memberId){
        boolean deleteSuccess = this.organizationMemberService.deleteByMemberId(memberId);
        if (!deleteSuccess){
            log.error("删除organization-member失败");
            return Result.error("删除organization-member失败");
        }
        log.info("删除organization-member成功");
        return Result.ok();
    }
    @PostMapping
    public Result<Void> addOrganizationMember(@RequestBody OrganizationMemberDTO organizationMemberDTO){
        OrganizationMember organizationMember = new OrganizationMember();
        BeanUtils.copyProperties(organizationMemberDTO,organizationMember);
        boolean addSuccess = this.organizationMemberService.save(organizationMember);
        if (!addSuccess){
            log.error("添加organization-member失败");
            return Result.error("添加organization-member失败");
        }
        log.info("添加organization-member成功");
        return Result.ok();
    }
}
